<template>
	<view class="container">
		<view class="topic-change">
			<view class="topic-back" @click="changeTopic('0')">上一题</view>
			<view class="topic-back" @click="changeTopic('1')">下一题</view>
		</view>
		<view class="topic-num">第{{currentTopic+1}}题</view>
		<view class="topic-area">
			<view class="topic-title">{{topics[currentTopic].title}}</view>
			<view class="topic-answer">
				<view :class="['answer-item', item.select&&'active']"
					v-for="(item, index) in topics[currentTopic].childred" :key="index" @click="selectTopic(item)">
					{{item.name}}
				</view>
			</view>
		</view>
		<view class="submit" @click="submit">提交</view>
	</view>
</template>

<script>
	import {
		topicList
	} from '@/common/data.js'
	export default {
		data() {
			return {
				// 题库
				topics: [],
				// 当前的题干
				currentTopic: 0,
				// 已选择的答案
				answers: []
			}
		},
		onLoad() {
			this.topics = topicList
		},
		methods: {
			selectTopic(item) {
				// 判断当前题是否已经选择过
				let curObj = this.topics[this.currentTopic]
				let answerItem = {
					title: curObj.title,
					value: item.name
				}
				curObj.childred.forEach(x => x.select = false)
				item.select = true
				const findRes = this.answers.find(x => x.title == curObj.title)
				if (findRes) {
					let finbIndex = this.answers.findIndex(x => x.title == curObj.title)
					this.answers.splice(finbIndex, 1, answerItem)
				} else {
					this.answers.push(answerItem)
				}
				setTimeout(() => {
					this.next()
				}, 100)
			},
			changeTopic(type) {
				if (type == '0') {
					if (!this.currentTopic == 0) {
						this.currentTopic--
					}
				} else {
					this.next()
				}
			},
			next() {
				if (this.currentTopic == this.topics.length - 1) {
					return
				}
				this.currentTopic++
			},
			submit() {
				console.log('answers', this.answers)
			}
		}
	}
</script>

<style lang="scss">
	.container {
		font-size: 14px;
		padding: 10px;

		.topic-change {
			display: flex;
			justify-content: space-between;
		}

		.topic-back {
			background: #409eff;
			width: 130rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #FFF;
			height: 30px;
			border-radius: 6rpx;
		}

		.topic-num {
			color: #F00000;
			font-size: 16px;
		}

		.topic-area {
			margin-top: 10px;

			.topic-title {
				font-weight: 600;
				font-size: 16px;
				text-indent: 16px;
			}

			.topic-answer {
				.answer-item {
					margin: 10px 0;

					&.active {
						color: #FFF;
						background: #409eff60;
					}
				}
			}
		}

		.submit {
			height: 80rpx;
			background-color: #409eff;
			color: #FFF;
			font-size: 16px;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 10rpx;
		}
	}
</style>